package com.ruoyi.health.report.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.health.report.domain.DifyVO;
import com.ruoyi.health.report.domain.HealthReport;
import com.ruoyi.health.report.domain.zkxc.ZkxcPushBaseEntity;
import com.ruoyi.health.report.service.IHealthReportService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 健康报告Controller
 *
 * @author luogongqun
 * @date 2024-09-16
 */
@RestController
@RequestMapping("/healthReport")
public class HealthReportController extends BaseController
{
    @Autowired
    private IHealthReportService healthReportService;

    /**
     * 查询健康报告列表
     */
    @RequiresPermissions("health:healthReport:list")
    @GetMapping("/list")
    public TableDataInfo list(HealthReport healthReport)
    {
        startPage();
        List<HealthReport> list = healthReportService.selectHealthReportList(healthReport);
        return getDataTable(list);
    }

    /**
     * 查询健康报告列表
     */
    @GetMapping("/wx/list")
    public TableDataInfo wxList(HealthReport healthReport)
    {
        startPage();
        if(StringUtils.isNull(healthReport.getCustomerId())){
            healthReport.setCustomerId(SecurityUtils.getUserId());
        }
        List<HealthReport> list = healthReportService.selectWxHealthReportList(healthReport);
        return getDataTable(list);
    }



    /**
     * 导出健康报告列表
     */
    @RequiresPermissions("health:healthReport:export")
    @Log(title = "健康报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HealthReport healthReport)
    {
        List<HealthReport> list = healthReportService.selectHealthReportList(healthReport);
        ExcelUtil<HealthReport> util = new ExcelUtil<HealthReport>(HealthReport.class);
        util.exportExcel(response, list, "健康报告数据");
    }

    /**
     * 获取健康报告详细信息
     */
    @RequiresPermissions("health:healthReport:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(healthReportService.selectHealthReportById(id));
    }

    /**
     * 新增健康报告
     */
    @RequiresPermissions("health:healthReport:add")
    @Log(title = "健康报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HealthReport healthReport)
    {
        return toAjax(healthReportService.insertHealthReport(healthReport));
    }

    /**
     * 修改健康报告
     */
    @RequiresPermissions("health:healthReport:edit")
    @Log(title = "健康报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HealthReport healthReport)
    {
        return toAjax(healthReportService.updateHealthReport(healthReport));
    }

    /**
     * 删除健康报告
     */
    @RequiresPermissions("health:healthReport:remove")
    @Log(title = "健康报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(healthReportService.deleteHealthReportByIds(ids));
    }

    @Log(title = "新增舌诊", businessType = BusinessType.INSERT)
    @PostMapping("glossoscopy")
    public AjaxResult glossoscopy(@RequestBody Map<String,String> map)
    {
        return success(healthReportService.glossoscopy(map));
    }

    @RequiresPermissions("health:healthReport:detail")
    @GetMapping(value = "getDetail/{id}")
    public AjaxResult getDetail(@PathVariable Long id){
        clearPage();
        return success(healthReportService.getDetail(id));
    }

    @GetMapping(value = "/wx/getDetail/{id}")
    public AjaxResult wxGetDetail(@PathVariable Long id){
        clearPage();
        HealthReport healthReport = new HealthReport();
        healthReport.setId(id);
        List<HealthReport> healthReports = healthReportService.selectWxHealthReportList(healthReport);
        if (healthReports.isEmpty()){
            throw new ServiceException("没有查看该数据的权限");
        }
        return success(healthReportService.getDetail(id));
    }

    @GetMapping(value = "/getLastReportInfo/{customerId}")
    public AjaxResult getLastReportInfo(@PathVariable(value = "customerId") Long customerId) {
        // 清理线程中可能残留的分页参数，避免影响当前查询
        clearPage();
        return success(healthReportService.getLastReportInfo(customerId));
    }

    @PostMapping(value = "/getLastReportInfoByDate")
    public AjaxResult getLastReportInfoByDate(@RequestBody DifyVO difyVO) {
         clearPage();
         return success(healthReportService.getLastReportInfoByDate(difyVO.getPhone(), difyVO.getDate(), "是".equals(difyVO.getWatchData())));
    }

    @GetMapping(value = "/reportInfoByMock/{phone}")
    public void getLastReportInfoByMock(@PathVariable(value = "phone") String phone) {
        healthReportService.getLastReportInfoByMock(phone);
    }

}
